package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.QueryPageBean;
import com.itheima.bean.Result;
import com.itheima.entity.Course;
import com.itheima.mapper.CourseMapper;
import com.itheima.service.ICourseService;
import com.itheima.entity.UResult;
import com.itheima.mapper.ResultMapper;
import com.itheima.utils.DateUtils;
import com.itheima.vo.courseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 学科表
 * 前端控制器
 * </p>
 *
 * @author itheima
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    /*    @Autowired
        private ICourseService courseService;*/
    @Autowired
    private CourseMapper courseMapper;


    @Autowired
    private ResultMapper resultMapper;

    /**
     * @description:分页
     * @author: Java
     * @date: 2021/3/28 13:14
     * @param: queryPageBean
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            //1. 构建分页对象
            Page<Course> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //2.调用业务查询
            IPage<Course> pageResult = courseMapper.selectPage(page, null);
            //3.封装到Result返回
            return new Result(true, "查询题库成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

    /**
     * @description:学生查看结果的分页
     * @author: Java
     * @date: 2021/3/28 13:14
     * @param: queryPageBean
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("findPageByUser")
    public Result findPageByUser(int userId, @RequestBody QueryPageBean queryPageBean) {
        try {
            //1. 构建分页对象
            Page<Course> page = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //2.创建查询条件构建器
            QueryWrapper<Course> wrapper = new QueryWrapper<>();
            //3.设置条件，仅展示已使用的题库。
            wrapper.eq("is_show", 1);
            //2.调用业务查询
            IPage<Course> pageResult = courseMapper.selectPage(page, wrapper);
            // 把里面的每道题找出来
            List<Course> records = pageResult.getRecords();
            // 遍历，然后每个都查一次。
            for (Course record : records) {
                int courseId = record.getId();
                // 构建条件
                LambdaQueryWrapper<UResult> Wrapper = new LambdaQueryWrapper<>();
                Wrapper.eq(UResult::getUserId, userId)
                        .eq(UResult::getCourseId, courseId);
                // 看看有没有
                List<UResult> resultList = resultMapper.selectList(Wrapper);
                // 如果没有，就是要去做，所以括号里面就是true，如果有就是已经做过了，直接看结果，所以括号里面就是flase
                record.setStatus(resultList.size() == 0);
            }
            // 把改好的放回去
            pageResult.setRecords(records);
            //3.封装到Result返回
            return new Result(true, "查询题库成功！", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }

    }

    /**
     * @description:禁用题库
     * @author: Java
     * @date: 2021/3/28 11:09
     * @param: id
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("EnableStatus")
    public Result EnableStatus(int id) {
        try {
            Course course = new Course();
            course.setId(id);
            course.setIsShow(1);
            //2.直接更新
            courseMapper.updateById(course);
            //3.响应
            return new Result(true, "启用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "启用失败");
        }
    }

    /**
     * @description:启用题库
     * @author: Java
     * @date: 2021/3/28 11:09
     * @param: id
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("DisableStatus")
    public Result DisableStatus(int id) {
        try {
            Course course = new Course();
            course.setId(id);
            course.setIsShow(0);
            //2.直接更新
            courseMapper.updateById(course);
            //3.响应
            return new Result(true, "禁用成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "禁用失败");
        }
    }

    /**
     * @description:删除试题
     * @author: Java
     * @date: 2021/3/28 11:21
     * @param: id
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("delete")
    public Result delete(int id) throws IOException {
        System.out.println("courseMapper里delete执行...id=" + id);
        //不讲道理，直接删
        courseMapper.deleteById(id);
        return new Result(true, "删除成功");
    }

    /**
     * @description:添加题库
     * @author: Java
     * @date: 2021/3/28 11:27
     * @param: course
     * @return: com.itheima.bean.UResult
     */
    @PostMapping("add")
    public Result add(@RequestBody Course course) {
        try {
            //1.创建日期
            course.setCreateDate(DateUtils.parseDate2String(new Date()));
            //2.设置权限，0是未启用
            course.setIsShow(0);
            //5.添加
            courseMapper.insert(course);
            //6.响应
            return new Result(true, "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "新增失败");
        }


    }
}
